package com.itmake.sample.springfox.config;

import com.itmake.sample.springfox.service.ISwaggerRouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.InMemorySwaggerResourcesProvider;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

import java.util.List;
import java.util.Objects;

/**
 * 重写接口提供
 *
 * @author Amjacks
 * @since 2023/08/10
 */
@Primary
@Component
public class SwaggerProvider implements SwaggerResourcesProvider {

    @Autowired(required = false)
    private ISwaggerRouteService swaggerRouteService;

    @Autowired
    private InMemorySwaggerResourcesProvider inMemorySwaggerResourcesProvider;

    /**
     * 若路由服务被实现，则聚合接口，否则使用默认的路由
     */
    @Override
    public List<SwaggerResource> get() {
        return Objects.isNull(swaggerRouteService) ? inMemorySwaggerResourcesProvider.get() : swaggerRouteService.getRoute();
    }
}
